$(document).ready(function(){ 
	
	
	$("#basketItemsWrap li:first").hide();
	$("#slidingTopContent").hide();
	var processDelete = function (productId){
		var data=new Array();
		var check = false;
		data[data.length]=new $OnlineStore.param('action','delete');
		data[data.length]=new $OnlineStore.param('id', productId);
		$.ajax({
				url : "/Product",
				type : "POST",
				data:data,
				async:false,
				success : function(data) {
					console.log(data)
					if (data=="Deleted"){
						check = true;
					}
					else{
						check = false;
					}
				}
		});
		return check;
	}
	var processUpdate = function (productId){
		var check = false;
		$.each($OnlineStore.products, function(i, product) {
		    if (product.id.id == productId) {
		    	var data=new Array();
		    	 data[data.length]=new $OnlineStore.param('action','update');
		    	 data[data.length]=new $OnlineStore.param('id', product.id.id);
		    	 data[data.length]=new $OnlineStore.param('owner', $OnlineStore.customer.email);
		    	 $.ajax({
		    			url : "/Product",
		    			type : "POST",
		    			data:data,
		    			async:false,
		    			success : function(data) {
		    				console.log(data)
		    				if (data == "Out of stock!!"){
		    					check = false;
		    				}else{
		    					var newItemObject = JSON.parse(data);
			    				$OnlineStore.newBasketItem = '<li id="productID_' + newItemObject.productId +'"><a href="no-js.htm" onClick="return false;"><img title="productBusket_'+newItemObject.id.id+'" src="images/delete.png" id="deleteProductID_' + newItemObject.productId + '"></a> ' +newItemObject.name+ '(' +newItemObject.numInStock+ ') - <span class="productPrice">' +$OnlineStore.previewCurrency(newItemObject.price)+ '</span></li>'
			    				check = true;
		    				}
		    			}
		    	});
		    	 return;
		    }
		});
		return check;
	}
	$(document).on("click", "#slidingTopTrigger", function(event) {
      	$("#slidingTopContent").slideToggle("slow", function(){
			if ($("#slidingTopContent").is(":visible")) {
				$("#slidingTopFooterLeft").html('<img src="images/arrow-up.png" alt="Hide Basket" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Hide Basket</a>');
			} else {
				$("#slidingTopFooterLeft").html('<img src="images/arrow-down.png" alt="Show Basket" /> <a href="no-js.htm" onclick="return false;" id="slidingTopTrigger">Show Basket</a>');
			}
		});
    }); 
	
	$(document).on("click", ".productPriceWrapRight a img", function(event) {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var newIitemObject = {};		
		if ($("#slidingTopContent").is(":visible")) {
			$("#notificationsLoader").html('<img src="images/loader.gif">');
			var checkstock = processUpdate(productIDVal);
			if (checkstock == false){
				$("#notificationsLoader").empty();
				alert("Out of stock!!");
				return;
			}
			console.log($OnlineStore.newBasketItem)
			$("#spanConfirm").show("slow");
			$("#confirm").show("slow");
			if( $("#productID_" + productIDVal).length > 0){			
				$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
				$("#productID_" + productIDVal).before($OnlineStore.newBasketItem).remove();
				$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
				$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);				
			} else {
				$("#basketItemsWrap li:first").before($OnlineStore.newBasketItem);
				$("#basketItemsWrap li:first").hide();
				$("#basketItemsWrap li:first").show("slow");  					
			}
			$("#notificationsLoader").empty();
		} else {			
			$("#slidingTopContent").slideToggle("slow", function(){																		 
				$("#slidingTopFooterLeft").html('<img src="images/arrow-up.png" alt="Hide Basket" /> <a href="aaa.htm" onclick="return false;" id="slidingTopTrigger">Hide Basket</a>');
				$("#notificationsLoader").html('<img src="images/loader.gif">');
				var checkstock = processUpdate(productIDVal);
				if (checkstock == false){
					$("#notificationsLoader").empty();
					alert("Out of stock!!");
					return;
				}
				console.log($OnlineStore.newBasketItem)
				$("#spanConfirm").show("slow");
				$("#confirm").show("slow");
				if( $("#productID_" + productIDVal).length > 0){
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
					$("#productID_" + productIDVal).before($OnlineStore.newBasketItem).remove();
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
					$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
					$("#notificationsLoader").empty();
					
				} else {
					$("#basketItemsWrap li:first").before($OnlineStore.newBasketItem);
					$("#basketItemsWrap li:first").hide();
					$("#basketItemsWrap li:first").show("slow");  
					$("#notificationsLoader").empty();			
				}
				$("#slidingTopTrigger").fadeTo(4000, 1, function(){
					$("#slidingTopContent").slideToggle("slow", function(){
						$("#slidingTopFooterLeft").html('<img src="images/arrow-down.png" alt="Show Basket" /> <a href="aaa.htm" onclick="return false;" id="slidingTopTrigger">Show Basket</a>');												 
					});
					
				});

			});												 
		}
				
	});
		
	$(document).on("click", "#basketItemsWrap li img", function(event) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var productInbusketSplitter 	= (this.title).split("_");
		var productInbusketId = productInbusketSplitter[1];
		$("#notificationsLoader").html('<img src="images/loader.gif">');
		var checkdelete = processDelete(productInbusketId);
		if (checkdelete == true){
			$("#productID_" + productIDVal).hide("slow",  function() {
				$(this).remove();
			});
			$("#notificationsLoader").empty();
		}
		$.getJSON("/Product?tbEmail="+$OnlineStore.customer.email+"&action=getsummary", function(jsonData){
			console.log(JSON.stringify(jsonData))
			$OnlineStore.productsInBusket = jsonData;
			if ($OnlineStore.productsInBusket.length == 0){
				$("#spanConfirm").hide("slow");
				$("#confirm").hide("slow");
			}
		});
	});

});
